/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Comment;
import com.blog.city.checkthedep.sessionbeans.CommentFacadeLocal;
import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedProperty;

/**
 *
 * @author Breath
 */
@ManagedBean
@RequestScoped
public class CommentBean {

    private Comment comment = new Comment();
    @EJB
    private CommentFacadeLocal commFac;
    @ManagedProperty(value = "#{articleBean}")
    private ArticleBean articleBean;
    private Integer artI;

    /**
     * 
     * @return
     */
    public ArticleBean getArticleBean() {
        return articleBean;
    }

    /**
     * 
     * @param articleBean
     */
    public void setArticleBean(ArticleBean articleBean) {
        this.articleBean = articleBean;
    }
    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    /**
     * 
     * @return
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * 
     * @param loginBean
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    /**
     * 
     */
    public void setAuthor() {
        comment.setRegisteredUserId(loginBean.getCheckedUser());
    }

    /**
     * 
     * @return
     */
    public CommentFacadeLocal getCommFac() {
        return commFac;
    }

    /**
     * 
     * @param commFac
     */
    public void setCommFac(CommentFacadeLocal commFac) {
        this.commFac = commFac;
    }

    /**
     * 
     * @return
     */
    public Comment getComment() {
        return comment;
    }

    /**
     * 
     * @param comment
     */
    public void setComment(Comment comment) {
        this.comment = comment;
    }

    /**
     * 
     * @param artI 
     * @return
     */
    public String commentOnArticle(Integer artI) {
        this.artI = artI;
        return "showComment";
    }

    /**
     * 
     * @return
     */
    public String persistComment() {
        if (comment != null) {
            setAuthor();
            articleBean.retrieveSelectedArticle(artI);
            comment.setArticleId(articleBean.getSelectedArticle());
            commFac.create(comment);
        }
        return "showAllArticleForComment";
    }

    /**
     * 
     * @return
     */
    public String persistEditedComment() {
        if (comment != null) {
            setAuthor();
            commFac.edit(comment);
        }
        return "showAllArticleForComment";
    }

    /**
     * 
     * @param id
     */
    public void deleteSComment(Integer id) {
        if (comment != null) {
            commFac.remove(comment);
        }
    }
}
